//
//  LzwMainViewController.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/3/31.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwMainViewController.h"

#import "CJRECMainControllerViewController.h"
#import "CJFoundMainController.h"
#import "CJDivsionMianViewController.h"
#import "CJBnimationMainViewController.h"
#import "CJSettingViewController.h"
#import <Masonry.h>
#import "LzwBaseViewController.h"
#import "LzwLeftViewController.h"

#import "LzwMHViewController.h"

#import "LzwSearchViewController.h"

#define MENUHEIGHT 35
#define MAXBLACKVIEWALPHA 0.5
#define BLACKVIEWSIZESCALE 0.75


static CGFloat const titleH = 44;
static CGFloat const navBarH = 64;
static CGFloat const maxTitleScale = 1.3;
#define MAXBLACKVIEWALPHA 0.5
#define BLACKVIEWSIZESCALE 0.75

#define YCKScreenW [UIScreen mainScreen].bounds.size.width
#define YCKScreenH [UIScreen mainScreen].bounds.size.height

@interface LzwMainViewController ()<JHViewControllerDelegate ,UIScrollViewDelegate,CJSettingViewControllerDeleGate>


//上面的view
@property (nonatomic, retain) UIView *topView;


@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
// 选中按钮
@property (nonatomic, weak) UIButton *selTitleButton;

@property (nonatomic, strong) NSMutableArray *buttons;

@property (nonatomic, strong) CJSettingViewController *settingVC;



@property (nonatomic, strong) UIPanGestureRecognizer* panG;


//view
@property (nonatomic, strong) UIView *viewleftSmall;

@property (nonatomic, strong) UIView *viewAlph;

//hei
//@property (nonatomic, strong) UIView *blackView;
@property (nonatomic, strong) UIButton *buttonBlack;


@property (nonatomic, assign) BOOL seleted;

@end

@implementation LzwMainViewController


//-(void)viewWillAppear:(BOOL)animated{
//    
//    [super viewWillAppear:animated];
//    //隐藏导航栏
////    self.navigationController.navigationBarHidden = YES;
//    
//   
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    //创建TopView
//    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, YCKScreenW, navBarH)];
//    self.topView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:self.topView];
    
    self.title = @"BiliBili";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    
    
    //创建抽屉 按钮
    [self creatLeftButton];
//创建收索按钮
    [self searchButton];
    
    //初始化数组
    self.buttons = [NSMutableArray array];
    
    
    [self setupTitleScrollView];//创建小scrollview
    [self setupContentScrollView];//创建大scrollview
    
    [self addChildViewController];//添加子控制器
    [self setupTitle];//根据子视图个数创建小scrollview的按钮
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * YCKScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.delegate = self;

//    [self creatGesture];
    
    
    self.viewleftSmall = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, SCREEN_HEIGHT)];
    self.viewleftSmall.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.viewleftSmall];
    
    
    [self.view bringSubviewToFront:self.viewleftSmall];
    
    _panG = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMove:)];
    [self.viewleftSmall addGestureRecognizer:_panG];
    
    
    
    //左侧隐藏的view
    self.viewAlph = [[UIView alloc]initWithFrame:CGRectMake(- SCREEN_WIDTH + 100, 0, SCREEN_WIDTH - 100, SCREEN_HEIGHT)];
//    self.viewAlph.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:self.viewAlph];
    
    CJSettingViewController *settingVc = [[CJSettingViewController alloc]init];
//    [self.navigationController addChildViewController:settingVc];
    settingVc.myDeleGate = self;
    ///添加VC生命周期
    [self addChildViewController:settingVc];
    [self.viewAlph addSubview:settingVc.view];
    
    self.buttonBlack = [[UIButton alloc]initWithFrame:CGRectMake(- SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _buttonBlack.backgroundColor = [UIColor colorWithWhite:0.919 alpha:1.000];
    _buttonBlack.alpha = 0.1;
    [self.view addSubview:_buttonBlack];
    
    [self.buttonBlack addTarget:self action:@selector(buttonBlackAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}



#pragma mark - 设置头部标题栏
- (void)setupTitleScrollView
{
    // 判断是否存在导航控制器来判断y值
//    CGFloat y = self.navigationControlle÷÷r ? navBarH : 0;
    CGRect rect = CGRectMake(0, 0, YCKScreenW, titleH);
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.backgroundColor = [UIColor colorWithRed:0.969 green:0.345 blue:0.533 alpha:1.000];
    titleScrollView.scrollEnabled = NO;
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
}

#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, YCKScreenW, YCKScreenH - navBarH);
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    //    contentScrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
}

#pragma mark - 添加子控制器
- (void)addChildViewController
{
//#import "CJRECMainControllerViewController.h"
//#import "CJFoundMainController.h"
//#import "CJDivsionMianViewController.h"
//#import "CJBnimationMainViewController.h"
//
    
    CJBnimationMainViewController *bnimaVc = [[CJBnimationMainViewController alloc]init];
    bnimaVc.title = @"番剧";
    [self addChildViewController:bnimaVc];
    
    CJRECMainControllerViewController *recVc = [[CJRECMainControllerViewController alloc]init];
    recVc.title = @"推荐";
    [self addChildViewController:recVc];
    
    CJDivsionMianViewController *divsionVc = [[CJDivsionMianViewController alloc]init];
    divsionVc.title = @"分区";
    [self addChildViewController:divsionVc];
    
    CJFoundMainController *foundVc = [[CJFoundMainController alloc]init];
    foundVc.title = @"发现";
    [self addChildViewController:foundVc];
    

}

#pragma mark - 设置标题
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = 100;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithWhite:0.885 alpha:1.000] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

// 按钮点击
- (void)chick:(UIButton *)btn
{
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * YCKScreenW;
    
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}
// 选中按钮
- (void)selTitleBtn:(UIButton *)btn
{
    [self.selTitleButton setTitleColor:[UIColor colorWithWhite:0.889 alpha:1.000] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selTitleButton = btn;
    [self setupTitleCenter:btn];
}

- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * YCKScreenW;
    
    UIViewController *vc = self.childViewControllers[i];
    
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, YCKScreenW, YCKScreenH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}

- (void)setupTitleCenter:(UIButton *)btn
{
    CGFloat offset = btn.center.x - YCKScreenW * 0.5;
    
    if (offset < 0)
    {
        offset = 0;
    }
    
    CGFloat maxOffset = self.titleScrollView.contentSize.width - YCKScreenW;
    if (offset > maxOffset)
    {
        offset = maxOffset;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger i = self.contentScrollView.contentOffset.x / YCKScreenW;
    [self selTitleBtn:self.buttons[i]];
    [self setUpOneChildViewController:i];
}

// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger leftIndex = offsetX / YCKScreenW;
    NSInteger rightIndex = leftIndex + 1;
    
    //    //(@"%zd,%zd",leftIndex,rightIndex);
    
    UIButton *leftButton = self.buttons[leftIndex];
    
    UIButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    CGFloat scaleR = offsetX / YCKScreenW - leftIndex;
    
    CGFloat scaleL = 1 - scaleR;
    
    
    CGFloat transScale = maxTitleScale - 1;
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    
    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    
    
    UIColor *rightColor = [UIColor colorWithRed:0.888 green:0.898 blue:0.920 alpha:1.000];
    UIColor *leftColor = [UIColor colorWithRed:0.888 green:0.898 blue:0.920 alpha:1.000];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}

////////////------------------//////////////
///边缘view的手势方法;
- (void)panMove:(UIPanGestureRecognizer*)sender{

    self.buttonBlack.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:1.0 animations:^{
        
        self.viewAlph.frame = CGRectMake(0, 0, SCREEN_WIDTH - 100, SCREEN_HEIGHT);
        
        [self.view addSubview:self.viewAlph];
        
    }];
    
}

///透明button的点击方法
- (void)buttonBlackAction:(UIButton *)btn{
    
    self.buttonBlack.frame = CGRectMake(- SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            [UIView animateWithDuration:1.0 animations:^{
    
            self.viewAlph.frame = CGRectMake(- SCREEN_WIDTH + 100, 0, SCREEN_WIDTH - 100, SCREEN_HEIGHT);
    
        }];
    
    
}

///创建左侧 按钮
- (void)creatLeftButton{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"Drawer.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(ButtonAction:)];
    
    
    
    
}

///开关左侧的view
- (void)ButtonAction:(UIBarButtonItem *)bart{
    
    
    if (self.seleted == NO) {
        [self panMove:nil];
    }
    else{
        [self buttonBlackAction:nil];
    }

   self.seleted = !self.seleted;
}

///tableview协议方法
- (void)pushText{
    
    //(@"---------");
    
    LzwMHViewController *mhVc = [[LzwMHViewController alloc]init];
    
    [self.navigationController pushViewController:mhVc animated:YES];
}

///创建收索按钮
- (void)searchButton{
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(0, 0, 20, 20);
////    self.navigationItem.rightBarButtonIt;
//    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"L15.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(SearchButtonAction:)];
//    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"L15.png"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 25, 25);
    
    [button addTarget:self action:@selector(RightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rigetItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem = rigetItem;
    

    
}

- (void)RightButtonAction:(UIButton *)bartIetm{
    
    
    LzwSearchViewController *searVc = [[LzwSearchViewController alloc]init];
    
    [self.navigationController pushViewController:searVc animated:YES];
}


@end
